package jinyilw.common;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class ArrayTools
{

	private ArrayTools()
	{
	}

	public static <T> List<T> getNew(List<T> newList, List<T> oldList)
	{
		if (newList != null && (oldList == null || oldList.isEmpty()))
			return newList;
		List<T> list = new ArrayList<T>();
		if (newList == null)
			return list;
		int i, size;
		for (T ne : newList)
		{
			size = oldList.size();
			T oe;
			for (i = 0; i < size; i++)
			{
				oe = oldList.get(i);
				if (ne.equals(oe))
				{
					oldList.remove(oe);
					break;
				}
			}
			if (i == size)
				list.add(ne);
		}
		return list;
	}

	public static <T> T[] newArrayByClass(Class<T> clazz, int length)
	{
		return (T[]) Array.newInstance(clazz, length);
	}

	public static <T> T[] copy(T[] src)
	{
		if (src == null)
			return null;
		T[] dest = (T[]) Array.newInstance(src.getClass().getComponentType(), src.length);
		System.arraycopy(src, 0, dest, 0, src.length);
		return dest;
	}

	public static <T> T[] copy(T[] src, T[] dest)
	{
		if (src == null)
			return null;
		if (dest == null)
			return copy(src);
		int l = Math.min(dest.length, src.length);
		System.arraycopy(src, 0, dest, 0, l);
		return dest;
	}

	public static Object[] copyArray(Object[] src)
	{
		if (src == null)
			return null;
		Object[] dest = new Object[src.length];
		System.arraycopy(src, 0, dest, 0, src.length);
		return dest;
	}

	public static int[] expandIntArray(int[] src, int increase)
	{
		int size = src.length;
		int[] dest = new int[size + increase];
		System.arraycopy(src, 0, dest, 0, size);
		return dest;
	}

	public static int[] expandIntArray(int[] src, int increase, boolean bottom)
	{
		int size = src.length;
		int[] dest = new int[size + increase];
		System.arraycopy(src, 0, dest, (bottom) ? 0 : increase, size);
		return dest;
	}
}
